//
//  JLXXDateView.m
//  MMPopupView
//
//  Created by apple on 2017/5/30.
//  Copyright © 2017年 LJC. All rights reserved.
//

#import "JLXXDateView.h"
#import <Masonry/Masonry.h>
#import "JLXXDatePickerView.h"
@interface JLXXDateView()

@property (nonatomic, strong) JLXXDatePickerView *datePickerView;

@property (nonatomic, strong) UIButton *btnCancel;

@property (nonatomic, strong) UIButton *btnConfirm;

@end

@implementation JLXXDateView

- (instancetype)init
{
    self = [super init];
    
    if ( self )
    {
        JLXXDateViewConfig *config = [JLXXDateViewConfig globalConfig];

        self.type = MMPopupTypeSheet;
        
        self.backgroundColor = [UIColor whiteColor];

        
        [self mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo([UIScreen mainScreen].bounds.size.width);
            make.height.mas_equalTo(config.datePickerViewHeight + config.buttonHeight);
        }];
        
        
        UIView *buttonView = [UIView new];
        buttonView.backgroundColor = config.buttonViewBackgroundColor;
        [self addSubview:buttonView];
        
        [buttonView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(config.buttonHeight);
            make.top.left.right.equalTo(self);
        }];
        
        self.btnCancel = [UIButton mm_buttonWithTarget:self action:@selector(actionHide:)];
        self.btnCancel.tag = 0;
        [buttonView addSubview:self.btnCancel];
        [self.btnCancel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(config.buttonWidth);
            make.left.top.bottom.equalTo(buttonView);
        }];
        [self.btnCancel setTitle:@"取消" forState:UIControlStateNormal];
        [self.btnCancel setTitleColor:config.buttonTitleColor forState:UIControlStateNormal];
        
        
        self.btnConfirm = [UIButton mm_buttonWithTarget:self action:@selector(actionHide:)];
        self.btnConfirm.tag = 1;
        [buttonView addSubview:self.btnConfirm];
        [self.btnConfirm mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(config.buttonWidth);
            make.right.top.bottom.equalTo(buttonView);
        }];
        [self.btnConfirm setTitle:@"确定" forState:UIControlStateNormal];
        [self.btnConfirm setTitleColor:config.buttonTitleColor forState:UIControlStateNormal];
        
        self.datePickerView = [JLXXDatePickerView new];
        [self addSubview:self.datePickerView];
        [self.datePickerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self).insets(UIEdgeInsetsMake(config.buttonHeight, 0, 0, 0));
        }];
    }
    
    return self;
}

- (void)actionHide:(UIButton *)btn
{
    if ( self.confirm && btn.tag ==1 )
    {
        self.confirm(self.datePickerView.selectedDate);
    }
    [self hide];
}

@end


@interface JLXXDateViewConfig()

@end

@implementation JLXXDateViewConfig

+ (JLXXDateViewConfig *)globalConfig
{
    static JLXXDateViewConfig *config;
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        
        config = [JLXXDateViewConfig new];
        
    });
    
    return config;
}

- (instancetype)init
{
    self = [super init];
    
    if ( self )
    {
        self.buttonHeight           = 50.0f;
        self.buttonWidth          = 80.0f;
        self.datePickerViewHeight   = 210.0;
        self.buttonFontSize         = 18.0f;
        self.buttonViewBackgroundColor  = MMHexColor(0xf1f1f1FF);
        self.buttonTitleColor       = MMHexColor(0x3393FFFF);
    }
    
    return self;
}
@end
